package utilidades;

import inicio.Principal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class TransfereArquivos {
  FTPClient ftp;


  public TransfereArquivos() {

  }


  public void init(String ftpServer, String user, String senha) {
    try {
      ftp = new FTPClient();
      ftp.connect(ftpServer);
      if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        if (ftp.login(user, senha))
          System.out.println(ftp.getReplyString());
        else {
          ftp.disconnect();
          System.out.println(ftp.getReplyString());

        }
      }
    }

    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }


  public void enviaCodigos(File args2, String pastaTrabalho) {
    String nomeArquivo = null;
    try {
      ftp.makeDirectory(Principal.pastaTrabalho);
      System.out.println(ftp.getReplyString());

      InputStream is = new FileInputStream(args2);

      nomeArquivo = args2.getName();

      ftp.setFileType(FTPClient.ASCII_FILE_TYPE);

      ftp.storeFile(Principal.pastaTrabalho + "/" + nomeArquivo, is);

      System.out.println(ftp.getReplyString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }


  public void enviaCodigos(File args2[], String pastaTrabalho, String projeto) {
    String nomeArquivo = null;

    try {

      ftp.makeDirectory(Principal.pastaTrabalho);
      System.out.println(ftp.getReplyString());

      ftp.makeDirectory(Principal.pastaTrabalho + "/" + projeto);
      System.out.println(ftp.getReplyString());

      for (int i = 0; i < args2.length; i++) {

        InputStream is = new FileInputStream(args2[i]);

        nomeArquivo = args2[i].getName();

        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);

        ftp.storeFile(Principal.pastaTrabalho + "/" + projeto + "/" + nomeArquivo, is);
        System.out.println(ftp.getReplyString());

      }

      ftp.disconnect();

    }
    catch (Exception e) {
      JOptionPane.showMessageDialog(null, " Falha na Na Conexão!!");
    }
  }


  public InputStream recuperaArquivo(String fileName) {
    InputStream in = null;

    try {
      in = ftp.retrieveFileStream(fileName);
      System.out.println(ftp.getReplyString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return in;

  }


  @SuppressWarnings("resource")
  public void recuperaProjet(String pastaTrabalho, String projeto) {
    InputStream inputStream = null;
    File f = new File(String.format("E:/RMI/GPUHelp-%s", pastaTrabalho));
    f.mkdir();
    f = new File(String.format("E:/RMI/GPUHelp-%s/%s", pastaTrabalho, projeto));
    f.mkdir();

    try {
      FTPFile[] file = ftp.listFiles(String.format("GPUHelp-%s/%s", pastaTrabalho, projeto));

      for (int i = 0; i < file.length; i++) {

        if (file[i].getName().matches(".+\\..+")) {
          inputStream = ftp.retrieveFileStream(String.format("GPUHelp-%s/%s/%s", pastaTrabalho, projeto, file[i].getName()));

          System.out.println(ftp.getReplyString());

          OutputStream outputStream = new FileOutputStream(new File(String.format("E:/RMI/GPUHelp-%s/%s/%s", pastaTrabalho, projeto, file[i].getName())));

          int read = 0;
          byte[] bytes = new byte[1024];

          while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
          }
        }
        inputStream = null;

      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }
}